﻿// PlcDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "PlcDlg.h"
#include "afxdialogex.h"


// CPlcDlg 对话框

IMPLEMENT_DYNAMIC(CPlcDlg, CDialogEx)

CPlcDlg::CPlcDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_PLC, pParent)
	, PLCWriteDB(0)
	, PLCReadDB(0)
	, WriteStart(0)
	, WriteMsg(0)
	, ReadStart(0)
	, check_mes(FALSE)
{

}

CPlcDlg::~CPlcDlg()
{
}

void CPlcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IP_PLC, PLCIP);
	DDX_Control(pDX, IDC_IP_30PLC, PLCIP_30);
	DDX_Text(pDX, IDC_EDIT_WRITE_DB, PLCWriteDB);
	DDX_Text(pDX, IDC_EDIT_READ_DB, PLCReadDB);
	DDX_Text(pDX, IDC_EDIT3, WriteStart);
	DDX_Text(pDX, IDC_EDIT14, WriteMsg);
	DDX_Text(pDX, IDC_EDIT15, ReadStart);
	DDX_Check(pDX, IDC_CHECK_MES, check_mes);
}


BEGIN_MESSAGE_MAP(CPlcDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CPlcDlg::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON1, &CPlcDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CPlcDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CPlcDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CPlcDlg 消息处理程序


BOOL CPlcDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	PLCIP.SetWindowText(gSystemMgr.PLC_IP);
	PLCIP_30.SetWindowText(gSystemMgr.PLC_IP_30);
	PLCWriteDB = gSystemMgr.Write_DB;
	PLCReadDB = gSystemMgr.Read_DB;
	
	check_mes = gSystemMgr.m_bMesCheck;
	UpdateData(FALSE);

	return TRUE; 
}


void CPlcDlg::OnBnClickedButtonSave()
{
	UpdateData(TRUE);

	PLCIP.GetWindowText(gSystemMgr.PLC_IP);
	PLCIP_30.GetWindowText(gSystemMgr.PLC_IP_30);
	gSystemMgr.Write_DB = PLCWriteDB;
	gSystemMgr.Read_DB = PLCReadDB;
	gSystemMgr.m_bMesCheck = check_mes;


	CString cstr;
	gSystemMgr.m_cPLCConfigIni.SaveConfigIni(L"PLC", L"PLC_IP", gSystemMgr.PLC_IP);
	gSystemMgr.m_cPLCConfigIni.SaveConfigIni(L"PLC", L"PLC_IP_30", gSystemMgr.PLC_IP_30);

	cstr.Format(L"%d", gSystemMgr.Write_DB);
	gSystemMgr.m_cPLCConfigIni.SaveConfigIni(L"PLC", L"Write_DB", cstr);

	cstr.Format(L"%d", gSystemMgr.Read_DB);
	gSystemMgr.m_cPLCConfigIni.SaveConfigIni(L"PLC", L"Read_DB", cstr);

	cstr.Format(L"%d", gSystemMgr.m_bMesCheck);
	gSystemMgr.m_cPLCConfigIni.SaveConfigIni(L"PLC", L"m_bMesCheck", cstr);
	
	gSystemLog.DisplayLogAndSave(L"PLC保存数据完成", LV_EVENT);
}


void CPlcDlg::OnBnClickedButton1()
{
	UpdateData(TRUE);
	if (gSystemMgr.PLCConnectFlag == FALSE) return;
	BYTE msg = WriteMsg;
	write_to_plc_db(gSystemMgr.Write_DB, WriteStart, 1, &msg);
	CString cstr;
	cstr.Format(L"Write to PLC[%d]:%d", WriteStart, msg);
	gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
}


void CPlcDlg::OnBnClickedButton2()
{
	UpdateData(TRUE);
	if (gSystemMgr.PLCConnectFlag == FALSE) return;
	BYTE msg;
	read_from_plc_db(gSystemMgr.Read_DB, ReadStart, 1, &msg);
	CString cstr;
	cstr.Format(L"Read from PLC[%d]:%d", ReadStart, msg);
	gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
}


void CPlcDlg::OnBnClickedButton3()
{
	return;
	/*if (gSystemMgr.PLCConnectFlag == FALSE) return;
	int status;
	CString cstr;
	BYTE length = 0;
	status = read_from_plc_db(gSystemMgr.Read_DB, gSystemMgr.PLC_R_SN + 1, 1, &length);
	status = read_from_plc_db(gSystemMgr.Read_DB, gSystemMgr.PLC_R_SN + 2, length, (BYTE*)gSystemMgr.SN);
	{
		USES_CONVERSION;
		cstr.Format(L"获取到条码[%d]:%s", length, A2T(gSystemMgr.SN));
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
	}
	sprintf(gSystemMgr.HolderSN, "%s", gSystemMgr.SN);*/
}
